package com.example.zheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zheng.entity.Counts;
import com.example.zheng.entity.GroupBuildingVo;
import com.example.zheng.entity.Repairbill;
import com.example.zheng.mapper.RepairbillMapper;
import com.example.zheng.service.IRepairbillService;
import com.example.zheng.util.GetStateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zheng
 * @since 2022-01-11
 */
@Service
@Transactional
public class RepairbillServiceImpl extends ServiceImpl<RepairbillMapper, Repairbill> implements IRepairbillService {
    @Autowired
    private RepairbillMapper repairbillMapper;

    public Map<String,Object> selectAllCountFromType(){
        HashMap map = new HashMap<String,Object>();

        List<Counts> countsList = repairbillMapper.selectAllCountFromType();

        List<Integer> counts = countsList.stream().map(c -> c.getTheCount()).collect(Collectors.toList());
        map.put("counts",counts);

        List<String> types = countsList.stream().map(c -> c.getType()).collect(Collectors.toList());
        map.put("types",types);
        return map;
    }

    @Override
    public Map<String, Object> selectAllCountAndState() {

        Integer total = repairbillMapper.selectCount(new QueryWrapper<>());

        Map<String,Object> map = new HashMap<>();
        ArrayList<Integer> countList = new ArrayList<>(Arrays.asList(0,0,0,0,0,0));
        ArrayList<Integer> percentList = new ArrayList<>(Arrays.asList(0,0,0,0,0,0));
        List<Counts> list = this.repairbillMapper.selectAllCountAndState();

        for (Counts count:list){
            countList.set(count.getState(),count.getTheCount());
            percentList.set(count.getState(), (int) ((float)count.getTheCount()/total*100));
        }

        ArrayList<String> stateList = new ArrayList<>(Arrays.asList("待分配", "待接单","维修中","待评价","异常单","已评价"));
        map.put("counts",countList);
        map.put("percents",percentList);
        map.put("states",stateList);

        return map;
    }

//    @Override
//    public Map<String,Object> selectAllCountAndState() {
//        Map<String,Object> map = new HashMap<>();
//        List<Counts> list = this.repairbillMapper.selectAllCountAndState();
//
//        List<String> states = list.stream().map(c -> GetStateUtil.getState(c.getState())).collect(Collectors.toList());
//        map.put("state",states);
//
//        List<Integer> counts = list.stream().map(c -> c.getTheCount()).collect(Collectors.toList());
//        map.put("counts",counts);
//
//        Integer total = repairbillMapper.selectCount(new QueryWrapper<>());
//
//        List<String> percents = list.stream().map(c -> {
//            System.out.println(new DecimalFormat("0.00").format((float)c.getTheCount()/48*100));   return new DecimalFormat("0.00").format((float)c.getTheCount()/48*100);}).collect(Collectors.toList());
//        map.put("percents",percents);
//        System.out.println("percents==="+percents);
//
//        return map;
//    }

    @Override
    public Map<String, Object> selectOneYearCountChange() {
        HashMap map = new HashMap<String,Object>();

        List<Counts> countsList = repairbillMapper.selectOneYearCountChange();

        List<Integer> counts = countsList.stream().map(c -> c.getTheCount()).collect(Collectors.toList());
        map.put("counts",counts);

        List<String> mouths = countsList.stream().map(c -> c.getMouth()).collect(Collectors.toList());
        map.put("mouths",mouths);
        return map;
    }

    @Override
    public Map<String, Object> selectOneYearSouthAndNotrhCountChange() {
        HashMap map = new HashMap<String,Object>();
        List<Counts> southList = repairbillMapper.selectOneYearSouthCountChange("南");
        List<Counts> northList = repairbillMapper.selectOneYearSouthCountChange("北");
        List<Integer> southcounts = southList.stream().map(c -> c.getTheCount()).collect(Collectors.toList());
        map.put("southcounts",southcounts);
        List<String> years = southList.stream().map(c -> c.getYear()).collect(Collectors.toList());
        map.put("years",years);
        List<Integer> northcounts = northList.stream().map(c -> c.getTheCount()).collect(Collectors.toList());
        map.put("northcounts",northcounts);
        return map;
    }

    @Override
    public List<GroupBuildingVo> selectCountFromGroupBuilding(){
        List<GroupBuildingVo> buildingVoList = repairbillMapper.selectCountFromGroupBuilding();
        System.out.println(buildingVoList);
        Integer other = repairbillMapper.selectCount(new QueryWrapper<>());
        for (GroupBuildingVo vo : buildingVoList) {
            other = other-vo.getValue();
        }
        GroupBuildingVo g = new GroupBuildingVo();
        g.setValue(other);
        g.setName("其他");
        buildingVoList.add(g);
        return buildingVoList;
    }

    @Override
    public List<Counts> selectCountFromGroupGrade(){
        return this.repairbillMapper.selectCountFromGroupGrade();
    }
}
